package com.rock.common.util;

import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.rock.common.base.BaseConstants;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 
 * @Description: Redis 工具类
 * @author: hotdog
 * @date: 2018年8月2日 下午4:36:18
 * @Copyright: v1.0
 *
 */
public class RedisUtil {

	protected static ReentrantLock lockPool = new ReentrantLock();
	protected static ReentrantLock lockJedis = new ReentrantLock();
	private static final Logger log = LoggerFactory.getLogger(RedisUtil.class);
	private static String IP = PropertiesFileUtil.getInstance(BaseConstants.CONFIG_FILENAME).get("master.redis.ip");
	private static int PORT = PropertiesFileUtil.getInstance(BaseConstants.CONFIG_FILENAME).getInt("master.redis.port");
	private static String PASSWORD = AESUtil.getInstance().decrypt((PropertiesFileUtil.getInstance(BaseConstants.CONFIG_FILENAME).get("master.redis.password")));
	private static int MAX_ACTIVE = PropertiesFileUtil.getInstance(BaseConstants.CONFIG_FILENAME).getInt("master.redis.maxActive");
	private static int MAX_IDLE = PropertiesFileUtil.getInstance(BaseConstants.CONFIG_FILENAME).getInt("master.redis.maxIdle");
	private static int MAX_WAIT = PropertiesFileUtil.getInstance(BaseConstants.CONFIG_FILENAME).getInt("master.redis.maxWait");
	private static int TIMEOUT = PropertiesFileUtil.getInstance(BaseConstants.CONFIG_FILENAME).getInt("master.redis.timeout");
	private static boolean TEST_ON_BORROW = false;
	private static JedisPool jedisPool = null;
	/**
	 * redis过期时间,以秒为单位
	 */
	public final static int EXRP_HOUR = 60 * 60;// 一小时
	public final static int EXRP_DAY = 60 * 60 * 24;// 一天
	public final static int EXRP_MONTH = 60 * 60 * 24 * 30;// 一个月

	/**
	 * 初始化Redis连接池
	 */
	private static void initialPool() {
		try {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxTotal(MAX_ACTIVE);
			config.setMaxIdle(MAX_IDLE);
			config.setMaxWaitMillis(MAX_WAIT);
			config.setTestOnBorrow(TEST_ON_BORROW);
			jedisPool = new JedisPool(config, IP, PORT, TIMEOUT);
		} catch (Exception e) {
			log.error("First create JedisPool error : " + e);
		}
	}

	/**
	 * 在多线程环境同步初始化
	 */
	private static synchronized void poolInit() {
		if (null == jedisPool) {
			initialPool();
		}
	}

	/**
	 * 同步获取Jedis实例
	 * 
	 * @return Jedis
	 */
	public synchronized static Jedis getJedis() {
		poolInit();
		Jedis jedis = null;
		try {
			if (null != jedisPool) {
				jedis = jedisPool.getResource();
				try {
					jedis.auth(PASSWORD);
				} catch (Exception e) {
				}
			}
		} catch (Exception e) {
			log.error("Get jedis error : " + e);
		}
		return jedis;
	}

	/**
	 * 设置 String
	 * 
	 * @param key
	 * @param value
	 */
	public synchronized static void set(String key, String value) {
		try {
			value = StringUtils.isBlank(value) ? "" : value;
			Jedis jedis = getJedis();
			jedis.set(key, value);
			jedis.close();
		} catch (Exception e) {
			log.error("Set key error : " + e);
		}
	}

	/**
	 * 设置 byte[]
	 * 
	 * @param key
	 * @param value
	 */
	public synchronized static void set(byte[] key, byte[] value) {
		try {
			Jedis jedis = getJedis();
			jedis.set(key, value);
			jedis.close();
		} catch (Exception e) {
			log.error("Set key error : " + e);
		}
	}

	/**
	 * 设置 String 过期时间
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 *            以秒为单位
	 */
	public synchronized static void set(String key, String value, int seconds) {
		try {
			value = StringUtils.isBlank(value) ? "" : value;
			Jedis jedis = getJedis();
			jedis.setex(key, seconds, value);
			jedis.close();
		} catch (Exception e) {
			log.error("Set keyex error : " + e);
		}
	}

	/**
	 * 设置 byte[] 过期时间
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 *            以秒为单位
	 */
	public synchronized static void set(byte[] key, byte[] value, int seconds) {
		try {
			Jedis jedis = getJedis();
			jedis.set(key, value);
			jedis.expire(key, seconds);
			jedis.close();
		} catch (Exception e) {
			log.error("Set key error : " + e);
		}
	}

	/**
	 * 获取String值
	 * 
	 * @param key
	 * @return value
	 */
	public synchronized static String get(String key) {
		Jedis jedis = getJedis();
		if (null == jedis) {
			return null;
		}
		String value = jedis.get(key);
		jedis.close();
		return value;
	}

	/**
	 * 获取byte[]值
	 * 
	 * @param key
	 * @return value
	 */
	public synchronized static byte[] get(byte[] key) {
		Jedis jedis = getJedis();
		if (null == jedis) {
			return null;
		}
		byte[] value = jedis.get(key);
		jedis.close();
		return value;
	}

	/**
	 * 删除值
	 * 
	 * @param key
	 */
	public synchronized static void remove(String key) {
		try {
			Jedis jedis = getJedis();
			jedis.del(key);
			jedis.close();
		} catch (Exception e) {
			log.error("Remove keyex error : " + e);
		}
	}

	/**
	 * 删除值
	 * 
	 * @param key
	 */
	public synchronized static void remove(byte[] key) {
		try {
			Jedis jedis = getJedis();
			jedis.del(key);
			jedis.close();
		} catch (Exception e) {
			log.error("Remove keyex error : " + e);
		}
	}

	/**
	 * lpush
	 * 
	 * @param key
	 * @param key
	 */
	public synchronized static void lpush(String key, String... strings) {
		try {
			Jedis jedis = RedisUtil.getJedis();
			jedis.lpush(key, strings);
			jedis.close();
		} catch (Exception e) {
			log.error("lpush error : " + e);
		}
	}

	/**
	 * lrem
	 * 
	 * @param key
	 * @param count
	 * @param value
	 */
	public synchronized static void lrem(String key, long count, String value) {
		try {
			Jedis jedis = RedisUtil.getJedis();
			jedis.lrem(key, count, value);
			jedis.close();
		} catch (Exception e) {
			log.error("lpush error : " + e);
		}
	}

	/**
	 * sadd
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 */
	public synchronized static void sadd(String key, String value, int seconds) {
		try {
			Jedis jedis = RedisUtil.getJedis();
			jedis.sadd(key, value);
			jedis.expire(key, seconds);
			jedis.close();
		} catch (Exception e) {
			log.error("sadd error : " + e);
		}
	}

	/**
	 * sadd
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 */
	public synchronized static void sadd(String key, String value) {
		try {
			Jedis jedis = RedisUtil.getJedis();
			jedis.sadd(key, value);
			jedis.close();
		} catch (Exception e) {
			log.error("sadd error : " + e);
		}
	}

	/**
	 * incr
	 * 
	 * @param key
	 * @return value
	 */
	public synchronized static Long incr(String key) {
		Jedis jedis = getJedis();
		if (null == jedis) {
			return null;
		}
		long value = jedis.incr(key);
		jedis.close();
		return value;
	}

	/**
	 * decr
	 * 
	 * @param key
	 * @return value
	 */
	public synchronized static Long decr(String key) {
		Jedis jedis = getJedis();
		if (null == jedis) {
			return null;
		}
		long value = jedis.decr(key);
		jedis.close();
		return value;
	}

	/**
	 * 设置key的过期时间
	 * 
	 * @param key
	 * @return expire
	 */
	public synchronized static void expire(String key, int expire) {
		try {
			Jedis jedis = getJedis();
			jedis.expire(key, expire);
			jedis.close();
		} catch (Exception e) {
			log.error("expire key error : " + e);
		}
	}

	/**
	 * 设置key的过期时间
	 * 
	 * @param key
	 * @return expire
	 */
	public synchronized static void expire(byte[] key, int expire) {
		try {
			Jedis jedis = getJedis();
			jedis.expire(key, expire);
			jedis.close();
		} catch (Exception e) {
			log.error("expire key error : " + e);
		}
	}

	/**
	 * 设置 hash值
	 * 
	 * @param key
	 * @param field
	 * @param value
	 * @return
	 */
	public synchronized static Long hset(byte[] key, byte[] field, byte[] value) {
		Jedis jedis = getJedis();
		if (null == jedis) {
			return null;
		}
		Long result = jedis.hset(key, field, value);
		jedis.close();
		return result;
	}

	/**
	 * 获取hash 域值
	 * 
	 * @param key
	 * @param field
	 * @return
	 */
	public synchronized static byte[] hget(byte[] key, byte[] field) {
		Jedis jedis = getJedis();
		if (null == jedis) {
			return null;
		}
		byte[] result = jedis.hget(key, field);
		jedis.close();
		return result;
	}

	/**
	 * 验证key 是否存在
	 * 
	 * @param key
	 * @return
	 */
	public synchronized static boolean exists(byte[] key) {
		Jedis jedis = getJedis();
		if (null == jedis) {
			return false;
		}
		boolean result = jedis.exists(key);
		jedis.close();
		return result;
	}
}